#pragma once

#include "NativeStructure.h"

using namespace System;
using namespace System::Drawing;

namespace Homography
{
	public ref class Homography
	{
	public:
		static array<float>^ ComputeHomography(array<float> ^ x1, array<float> ^ y1, array<float> ^ x2, array<float> ^ y2)
		{
			array<float>^ rs = gcnew array<float>(9);
			pin_ptr<float> px1 = &x1[0];
			pin_ptr<float> px2 = &x2[0];
			pin_ptr<float> py1 = &y1[0];
			pin_ptr<float> py2 = &y2[0];
			auto mat = ::ComputeHomography(px1, py1, px2, py2);
			for (int i = 0; i<9; i++)
				rs[i] = mat.m[i];
			return rs;
		}
		static Bitmap^ RectifyImage(Bitmap ^ imIn, array<float> ^ x, array<float> ^ y)
		{
			Bitmap^ rs = gcnew Bitmap(1024, 1024);
			::Image input, output;
			input.Width = imIn->Width;
			input.Height = imIn->Height;
			auto data = imIn->LockBits(Rectangle(0,0,imIn->Width, imIn->Height), System::Drawing::Imaging::ImageLockMode::ReadOnly,
				System::Drawing::Imaging::PixelFormat::Format32bppArgb);
			input.Pixels = (unsigned int*)(void*)(data->Scan0);
			auto buffer = rs->LockBits(Rectangle(0,0,rs->Width, rs->Height), System::Drawing::Imaging::ImageLockMode::WriteOnly,
				System::Drawing::Imaging::PixelFormat::Format32bppArgb);
			output.Pixels = (unsigned int*)(void*)(buffer->Scan0);
			output.Width = 1024;
			output.Height = 1024;
			pin_ptr<float> px = &x[0];
			pin_ptr<float> py = &y[0];
			::Rectify(input, output, px, py);
			imIn->UnlockBits(data);
			rs->UnlockBits(buffer);
			return rs;
		}
	};
}
